package com.gxa.modules.sys.controller;


import com.gxa.common.utils.PageUtils;
import com.gxa.common.utils.Result;
import com.gxa.modules.sys.dto.CouponAddDto;
import com.gxa.modules.sys.dto.CouponQueryDto;
import com.gxa.modules.sys.entity.Coupon;
import com.gxa.modules.sys.service.CouponService;
import io.swagger.annotations.*;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.*;

@Api(tags = "卡劵购买管理接口")
@RestController
@Slf4j
public class CouponController {

    @Autowired
    private CouponService couponService;



    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "page",value ="当前是第几页",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "limit",value ="每页显示多少条",dataType ="int"),
    })
    @ApiOperation(value="卡劵查询接口")
    @GetMapping("/coupon/list")
    public Result queryCoupon(@RequestParam @ApiIgnore Map<String,Object> params){
//        try {
//            Date date = new Date(System.currentTimeMillis());
//            CouponQueryDto coupon = new CouponQueryDto("单人区","一小时卡",date,12,1);
//            CouponQueryDto coupon01 = new CouponQueryDto("双人区","一小时卡",date,22,1);
//            List<CouponQueryDto> couponList = new ArrayList<>();
//            couponList.add(coupon);
//            couponList.add(coupon01);
//            Map map = new HashMap();
//            map.put("coupon",couponList);
//
//            return new Result().ok(map);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new Result().error("查询失败");
//        }
        try {
            PageUtils pageUtils = this.couponService.queryCoupon(params);
            return new Result<PageUtils>().ok(pageUtils);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result().error("查询失败");
        }
    }


    @ApiOperation(value="卡劵添加接口")
    @PostMapping("/coupon/add")
    public Result addCoupon(@RequestBody CouponAddDto couponAddDto){
        log.info("------------{couponAddDto}------------",couponAddDto);
        try {
            Date date = new Date(System.currentTimeMillis());
            couponAddDto.setCreatTime(date);

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DATE,couponAddDto.getValidDate()+1);
            Date expireTime = calendar.getTime();
            couponAddDto.setExpireTime(expireTime);

            this.couponService.addCoupon(couponAddDto);
            return new Result().ok();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result().error("添加失败");
        }
    }



    @ApiOperation(value="卡劵删除和批量删除接口")
    @DeleteMapping("/coupon/delete")
    public Result deleteCoupon(@RequestBody List<Integer> id){

        try {
            this.couponService.deleteCoupon(id);
            return new Result().ok();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result().error("删除失败");
        }
    }



    @ApiOperation(value="卡劵价格修改接口")
    @PutMapping("/coupon/updatePrice")
    public Result updatePrice(@RequestBody Coupon coupon){
        try {
            this.couponService.updatePrice(coupon);
            return new Result().ok();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result().error("修改失败");
        }
    }



    @ApiOperation(value="卡劵恢复售卖和批量恢复售卖接口")
    @PutMapping("/coupon/updateSell")
    public Result updateSell(@RequestBody List<Coupon> couponList){
        try {
            this.couponService.updateSell(couponList);
            return new Result().ok();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result().error("修改失败");
        }
    }


    @ApiOperation(value="卡劵停止售卖和批量停止售卖接口")
    @PutMapping("/coupon/updateHaltSales")
    public Result updateHaltSales(@RequestBody List<Coupon> couponList){
        try {
            this.couponService.updateHaltSales(couponList);
            return new Result().ok();
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result().error("修改失败");
        }
    }
}
